﻿//-----------------------------------------------------------------------
// <copyright file="Connection.cs" company="Craig Longford">
//     Copyright (c) Craig Longford. All rights reserved.
//     Email: deltawolf7@gmail.com
//     WWW: http://www.deltasblog.co.uk
// </copyright>
//-----------------------------------------------------------------------

namespace Delta.Data.DataExpress
{
    using System.Configuration;
    using System.Data;

    /// <summary>
    /// Connection settings
    /// </summary>
    public class Connection
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the Connection class.
        /// </summary>
        /// <param name="connectionString">Database connection string</param>
        /// <param name="providerName">Database provider name</param>
        public Connection(string connectionString, string providerName)
        {
            this.ConnectionString = connectionString;
            this.ProviderName = providerName;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the database connection string
        /// </summary>
        public string ConnectionString { get; set; }

        /// <summary>
        /// Gets or sets the database provider name
        /// </summary>
        public string ProviderName { get; set; }
        #endregion

        #region Create Connection Method
        /// <summary>
        /// Create a connection
        /// </summary>
        /// <returns>Database connection</returns>
        public IDbConnection GetConnection()
        {
            if (string.IsNullOrEmpty(this.ConnectionString))
            {
                throw new DataExpressException("No connection string defined");
            }

            if (string.IsNullOrEmpty(this.ProviderName))
            {
                throw new DataExpressException("No provider name definded");
            }

            ConnectionStringSettings settings = new ConnectionStringSettings();
            settings.ProviderName = this.ProviderName;
            settings.ConnectionString = this.ConnectionString;

            return Helper.GetDatabaseType(settings);
        }
        #endregion
    }
}
